package com.zxy.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zxy.common.ResultData;
import com.zxy.dto.VolunteerInfo;
import com.zxy.entity.Volunteer;
import com.zxy.mapper.VolunteerMapper;
import com.zxy.service.IVolunteerService;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import javax.xml.transform.Result;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author Zxy
 * @since 2021-05-11
 */
@RestController
@RequestMapping("/volunteer")
@AllArgsConstructor
public class VolunteerController {

    private final IVolunteerService volunteerService;
    private final VolunteerMapper volunteerMapper;

    // 查询全部志愿者
    @GetMapping("/findall")
    public ResultData findAll(@RequestParam(defaultValue = "1") Integer current, @RequestParam(defaultValue = "10") Integer size) {
        Page<VolunteerInfo> page = new Page<>(current, size);
        IPage<VolunteerInfo> volunteerPage = volunteerMapper.selectVolunteerPage(page);
        return ResultData.success(volunteerPage);
    }

    // 删除志愿者
    @DeleteMapping("/deletebyid")
    public ResultData deleteById(Integer stuId) {
        QueryWrapper<Volunteer> wrapper = new QueryWrapper<>();
        wrapper.eq("stu_id", stuId);
        // 删除志愿者信息
        volunteerService.remove(wrapper);
        return ResultData.success(1);
    }

    // 新增志愿者
    @PostMapping("/addvolun")
    public ResultData insertVolun(@RequestBody Volunteer volunteer) {
        volunteerService.save(volunteer);
        return ResultData.success(volunteer);
    }

}
